// packageSquare/AppointmentActivity/index.js
import {
  request
} from '../../utils/request'
import {
  isLogin
} from '../../utils/isLogin'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    typeList: [],
    activityRecommendations: {},
    freeCourses: [],
    activeType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    isLogin()
  },

  /**
   * 获取免费课程
   */
  getFreeCourses() {
    request({
      url: '/mini/api/square/freeCourses',
      method: 'POST',
      data: {
        type: this.data.activeType
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          freeCourses: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 获取赛事榜单
   */
  getEventRanking() {
    request({
      url: '/mini/api/square/competitionRanking',
      method: 'POST',
      data: {
        type: this.data.activeType
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          eventRanking: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getTypeList() {
    request({
      url: '/mini/api/makeActivity/findType'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          typeList: resdata.data || []
        })
        this.setData({
          activeType: resdata.data[0].code
        })
        this.getFreeCourses()
        this.getEventRanking()
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toRankingDetail(item) {
    wx.navigateTo({
      url: '/packageSquare/rankingDetail/rankingDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toCoursesDetail(item) {
    wx.navigateTo({
      url: '/packagePlan/coursesDetail/coursesDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  onChange({
    detail
  }) {
    this.setData({
      activeType: detail.name
    })
    this.getFreeCourses()
    this.getEventRanking()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTypeList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})